package com.cszjzx.member.dao;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import com.cszjzx.member.po.Member;
import com.cszjzx.member.po.TempleMember;


public class TempleMemberDAOImpl implements TempleMemberDAO,RowMapper{
	private JdbcHelper helper;
    public TempleMemberDAOImpl() throws IOException {
    	helper=new JdbcHelper();
    }
    
	public void add(TempleMember templeMember) throws SQLException {
		int p = Integer.parseInt(templeMember.getMember().getValue("process"));
		if(p == 1){
			String s1="submittime,operationtype,masterid,checkcondition,"+Member.name[0];   
			String s2=" value(?,?,?,?,?";
			for(int i=1;i<Member.name.length;i++){
				s1+=","+Member.name[i];
				s2+=",?";
			}
			@SuppressWarnings("unused")
			////
			String process = templeMember.getMember().getValue("process");
			String sql = "insert into templemember (" +s1+")" + s2+")";
			Object [] args = new Object[Member.name.length+4];
			args[0]=new java.sql.Date(templeMember.getSubmitTime().getTime());
			args[1]=templeMember.getOperationType();
			args[2]=templeMember.getMasterid();
			args[3]=templeMember.getCheckcondition();
			for(int i=4;i<args.length;i++){
				args[i]=templeMember.member.getValue(i-4);
			}
			helper.executeUpdate(sql, args);
		}
		else{
			try {
				new TempleMemberDAOImpl().update(templeMember);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
	@SuppressWarnings("static-access")
	public Object mapRow(ResultSet rs) throws SQLException, ClassNotFoundException, IOException {
		TempleMember templeMember=new TempleMember();
		templeMember.setSubmitTime(rs.getDate(1));
		templeMember.setOperationType(rs.getInt(2));
		templeMember.setMasterid(rs.getInt(3));
		templeMember.setCheckcondition(rs.getInt(4));
		for(int i=0;i<templeMember.member.name.length;i++){
			templeMember.member.setValue(i, rs.getString(i+5));
		}
		return templeMember;
	}
	@SuppressWarnings("unchecked")
	public List<TempleMember> findByCheckCondition(int checkcondition) throws ClassNotFoundException, SQLException, IOException{
	    String sql;
	    Object []args={};
	    if(checkcondition==0){
	    	sql ="select * from templemember order by submittime";
	    	
	    }else{
	    	sql ="select * from templemember where checkcondition="+checkcondition+" order by submittime";
	    }
		return helper.executeQuery(sql, args, this);
	}
	
	public TempleMember findByIdcard(String idcard) throws ClassNotFoundException, SQLException, IOException{
		String sql = "select * from templemember where idcard = ? order by submittime";
		Object []args={idcard};
		return (TempleMember) helper.executeQuery(sql, args, this).get(0);
	}
	
	@SuppressWarnings("unchecked")
	public List<TempleMember> findByCheckConditionAndMasterid(int masterid,int checkcondition) throws ClassNotFoundException, SQLException, IOException{
		String sql;
	    Object []args={checkcondition,masterid};
	    /*if(checkcondition==0){
	    	sql ="select * from templemember where masterid = ? order by submittime ";
	    	
	    }else{
	    	sql ="select * from templemember where checkcondition="+checkcondition+" and where masterid = ? order by submittime ";
	    }*/
	    sql ="select * from templemember where checkcondition=? and masterid = ? order by submittime ";
		return helper.executeQuery(sql, args, this);
	}
    public void addList(List<TempleMember> templeMembers) {
		
	}
    @SuppressWarnings("static-access")
	public void  update(TempleMember templeMember) throws SQLException{
    	//update templemember 
    	String sql ="update templemember set submittime=?,operationtype=?,masterid=?,checkcondition=?";
    	Object[]args=new Object[4+Member.name.length];
    	args[0]=new java.sql.Date(templeMember.getSubmitTime().getTime());
    	args[1]=templeMember.getOperationType();
    	args[2]=templeMember.getMasterid();
    	args[3]=templeMember.getCheckcondition();
    	for(int i=0,j=0;i<templeMember.member.name.length;i++){
    		String value = templeMember.member.getValue(i);
    		if(templeMember.member.name[i].equals("idcard")){
    			continue;
    		}
    		if(value == null || value.equals("")){
    			value = null;
    		}
    		sql+=","+templeMember.member.name[i]+"=?";
    		args[j+4]=value;
    		j++;
    	}
    	sql += " where idcard = ?";
    	args[3+Member.name.length] = templeMember.getMember().getValue("idcard");
    	System.out.println(args[3+Member.name.length]);
    	helper.executeUpdate(sql, args);
    }
    private void ChangCheckcondition(String idcard,int checkCondition) throws SQLException{
    	String sql="update templemember set checkcondition=? where idcard=?";
    	Object []args={checkCondition,idcard};
    	helper.executeUpdate(sql, args);
    }
    public void update(List<TempleMember> templeMembers) throws SQLException, IOException, ClassNotFoundException {
    	for(int i=0;i<templeMembers.size();i++){
    			//第一步判断是否审核状态
    		switch(templeMembers.get(i).getCheckcondition()){
     		case 2:
     			//对于审核通过的要进行数据的迁移
     			switch (templeMembers.get(i).getOperationType()) {
				case 0:
					new MemberDAOImpl().add(templeMembers.get(i).member);
					break;
				case 1:
					new MemberDAOImpl().update(templeMembers.get(i).member);
					break;
				case 2:
					new MemberDAOImpl().delete(templeMembers.get(i).member.getValue(0));
					break;
				}break;
     		case 0:break;
    		case 1:break;
    		case 3: new TempleMemberDAOImpl().ChangCheckcondition(templeMembers.get(i).getMember().getValue("idcard"),templeMembers.get(i).getCheckcondition());break;
    		}
    	}
	}
    public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException {
/*		List list =new TempleMemberDAOImpl().findByCheckCondition(1);
		for(int i=0;i<list.size();i++){
			System.out.println(list.get(i));
		}	
		TempleMember templeMember =(TempleMember) list.get(0);
		templeMember.member.setValue("idcard", "909090");
		TempleMember templeMember=(TempleMember) list.get(0);
		templeMember.setCheckcondition(2);
		List list2=new ArrayList();*/
		//list2.add(templeMember);
//		new TempleMembleDAOImpl().update((list2));
	}
}
